package org.bao.service;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.taobao.api.ApiException;
import org.bao.bean.alertBean;
import org.bao.bean.alertMessagesBean;
import org.bao.controller.ReceiveMsgController;
import org.bao.response.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：feller
 * @date ：Created in 2022/05/12 17:47
 * @description：对接受到的消息进行处理
 * @version: 1.0
 */
@Service
public class ProcessMsg {
    private final Logger logger = LoggerFactory.getLogger(ReceiveMsgController.class);

    public Response checkMsg(String msg) throws ApiException {

        PostDingtalkService postDingtalkService = new PostDingtalkService();

        Boolean flag = msg.contains("isMock");//判断是否是测试消息
        if (flag){
            return Response.success();
        }else{
            Gson gson = new Gson();
            alertBean alertBean = gson.fromJson(msg, new TypeToken<alertBean>() {}.getType());

            alertMessagesBean alertMessages = alertBean.getMessages().get(0);//获取告警详情,只取第一个
            logger.info("发送钉钉消息开始 -> "+alertMessages.toString());

            return postDingtalkService.setMsg(alertMessages);
        }
    }
}
